package org.jackysoft.struts2.workflow;

import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.jackysoft.entity.SystemService;
import org.jackysoft.entity.SystemServiceSetting;
import org.jackysoft.entity.User;
import org.jackysoft.entity.Variable;
import org.jackysoft.entity.WorkflowEntry;
import org.jackysoft.opensymphony.workflow.entity.ActionDescriptor;
import org.jackysoft.opensymphony.workflow.entity.WorkflowDescriptor;
import org.jackysoft.service.WorkflowService;
import org.jackysoft.util.WorkflowConstant;
import org.jackysoft.workflow.ActionItem;
import org.jackysoft.workflow.WorkItem;
import org.joda.time.DateTime;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;


/*******************************************************************************
 * 执行工作流的步骤
 * 
 * @author 屈甲康
 * @QQ 285799123
 * 
 */

@Namespace("/workflow/proceed")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@InterceptorRefs({ @InterceptorRef("defaultStack"),
		@InterceptorRef("annotationWorkflow") })
public class ProceedAction extends AbstractWorkflowAction<WorkItem> {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8455738459445280437L;
	private List<ActionItem> actions;
	
	
	private SystemService sysService;	
	private boolean auto = false;

	public String proceed() throws Exception {
		User user = getLoginUser();
		Map<String, Object> input = new HashMap<String, Object>();
		if (actionId != -1) {			
			workflow.doAction(workflowId, actionId, input,nextOwners);

		}
		WorkflowDescriptor desc = workflowService
				.getWorkflowDescriptor(workflow.getWorkflowName(workflowId));

		ActionDescriptor actor = desc.getAction(actionId);
	   
		variableService.setVariable(workflowId,
				WorkflowConstant.ACTION_ID.toString(), "" + actionId);

		variableService.setVariable(workflowId,
				WorkflowConstant.ACTION_NAME.toString(), actor == null ? ""
						: actor.getName());

		variableService.setVariable(workflowId,
				WorkflowConstant.SEND_DATE.toString(),
				DateTime.now());

		variableService.setVariable(workflowId,
				WorkflowConstant.LAST_EXECUTOR.toString(), user.getLocalName());
		variableService.setVariable(workflowId, 
				WorkflowConstant.SENDER.toString(), user.getLocalName());
		variableService.setVariable(workflowId, 
				WorkflowConstant.SENDER_USERID.toString(), user.getUsername());
		variableService.setVariable(workflowId,
				WorkflowConstant.LAST_EXECUTOR_USERID.toString(), user.getUsername());
		WorkflowService.LOCAL_STATUS.set(WorkflowConstant.STATE_UNDERWAY);
		return !auto?SUCCESS:"auto";
	}


	public String prework() throws Exception {
		Variable actionNameVar = variableService.get(workflowId, WorkflowConstant.ACTION_NAME.toString());
		Variable actionIdvar = variableService.get(workflowId, WorkflowConstant.ACTION_ID.toString());
		if(actionNameVar!=null && actionIdvar!=null ){
			actionId = Integer.parseInt(actionIdvar.getValue());
			actionName=actionNameVar.getValue();
			String name = workflow.getWorkflowName(workflowId);
			SystemServiceSetting	sysServiceSetting =  sysServiceSettingService.get(name, actionId);
			sysService = sysServiceSetting==null?null:sysServiceSetting.getSysService();
		}		
		
		
		return "prework";
	}
		
	public String actionform() throws Exception{
		//actionItem = workflow.getActionItemById(actionId, workflowId, Collections.EMPTY_MAP);
		actions = workflow.getActionItem(workflowId, Collections.EMPTY_MAP);
		return "actionform";
	}
	


	
	public String rollback() throws Exception {
		workflowService.rollbackWorkflow(workflowId);
		return SUCCESS;
	}
	
	public String complete() throws Exception {
		
		workflow.changeEntryState(workflowId,WorkflowEntry.COMPLETED);
		//workflowService.completeWorkflow(workflowId);
		return SUCCESS;
	}

	
	public String delete() throws Exception {
		workflow.changeEntryState(workflowId,WorkflowEntry.KILLED);
		//workflowService.deleteWorkflow(workflowId);
		return SUCCESS;
	}

	
	

	public void setSysService(SystemService sysService) {
		this.sysService = sysService;
	}

	public SystemService getSysService() {
		return sysService;
	}


	public boolean isAuto() {
		return auto;
	}


	public void setAuto(boolean auto) {
		this.auto = auto;
	}
	public List<ActionItem> getActions() {
		return actions;
	}

	public void setActions(List<ActionItem> actions) {
		this.actions = actions;
	}	
	
	
}
